package com.assessment.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.assessment.entity.PerformanceInterview;
import com.fasterxml.jackson.annotation.JsonFormat;


public class PerformanceInterviewDTO 
{
	private Long id;
	private Long staffId;
	private String staffName;
	private Long superiorId;
	private String superiorName;
	private String title;
	private String content;
	private Date talkTime;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getStaffId() {
		return staffId;
	}
	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}
	public String getStaffName() {
		return staffName;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	public Long getSuperiorId() {
		return superiorId;
	}
	public void setSuperiorId(Long superiorId) {
		this.superiorId = superiorId;
	}
	public String getSuperiorName() {
		return superiorName;
	}
	public void setSuperiorName(String superiorName) {
		this.superiorName = superiorName;
	}
	
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getTalkTime() {
		return talkTime;
	}
	public void setTalkTime(Date talkTime) {
		this.talkTime = talkTime;
	}
	public static Specification<PerformanceInterview> getSpecification( final PerformanceInterviewDTO performanceInterviewDTO)
	{
		Specification<PerformanceInterview> spec = new Specification<PerformanceInterview>() {
			public Predicate toPredicate(Root<PerformanceInterview> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != performanceInterviewDTO && !StringUtils.isEmpty(performanceInterviewDTO.getStaffId())) {
					 Predicate  p1 =  cb.like(root.get("staff.id").as(String.class),"%"+ performanceInterviewDTO.getStaffName() + "%");
					 list.add(p1);
				 }
				 if(null != performanceInterviewDTO && !StringUtils.isEmpty(performanceInterviewDTO.getSuperiorId())) {
					 Predicate  p2 =  cb.like(root.get("superior_id").as(String.class),"%"+ performanceInterviewDTO.getSuperiorName()+ "%");
					 list.add(p2);
				 }
				 if(null != performanceInterviewDTO && !StringUtils.isEmpty(performanceInterviewDTO.getTitle())) {
					 Predicate  p2 =  cb.like(root.get("title").as(String.class),"%"+ performanceInterviewDTO.getTitle()+ "%");
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;	
	}
	
}
